<?php

class Push_channel_Controller Extends Controller {

    private $type_object;
	private $keeptimelist;	//保存时间
	
    function _init() {
        check_level(50, segment(0), segment(1, 'index'));
        $this->platform=array('IOS'=>'IOS','Android'=>'Android','Win32'=>'Win32');
        $this->channel = array('All'=>'All','AppStore' => 'AppStore', 
        		'91' => '91', 'PP' => 'PP','aiwangyou'=>'aiwangyou',
        		'uc'=>'uc' , 'kuaiyong' => 'kuaiyong' , 
        		'itools' => 'itools' , "duoku" => "duoku" , 
        		"dangle" => "dangle" );
        $this->frequence = array('once' => translate('ONCE'),  'now' => translate('RIGHT_NOW'));
    }

    function index() {
    	set_area_list_view("arealist");
    	//$this->view->assign("platform", $this->form->select("row[platform]", $this->platform, '', array('id' => 'platform')));
    	$this->view->assign("channel", $this->form->select("row[channel]", $this->channel, '', array('id' => 'channel')));
    	$this->view->assign("frequence", $this->form->select("row[frequence]", $this->frequence, array(), array('id' => 'frequence')));
        $this->view->display("push/push_channel/index");
        if (is_submit()) {
        	set_time_limit(0);
        	$rowdata = $_POST['row'];
        	$areaId = $_POST['areaIds'];
        	if ($rowdata['frequence'] == 'now') {
        		//插入需要推送的token
        		//var_dump($this->db->connectDetail());
        		$this->clear_tmp_token();
        		if ($rowdata['channel'] == 'All') {
        			$sql = "insert tmp_token (token) SELECT token FROM tt_role_token";
        		}
        		else {
        			$sql = "insert tmp_token (token) SELECT token FROM tt_role_token where channel = ".$rowdata['channel'];
        		}
        		$insertRes = $this->db->query($sql);
        		include_once 'fsocket.php';
        		$pushByToken = new Push_By_Token_Controller($this->db);
        		$pushByToken->push($rowdata['content']['content']);
        		echo "<script>alert('success');</script>";
        	}
        	else {
        		$insertData = array();
        		$insertData['channel'] = $rowdata['channel'];
        		$insertData['content']  = $rowdata['content']['content'];
        		$insertData['starttime'] = strtotime($rowdata['starttime']);
        		$insertData['type'] = 4;
        		$insertRes = $this->db->insert('push_message',$insertData);
        		if ($insertRes === false) {
     				echo "<script>alert('fail');</script>";
     			}
        		else {
        			echo "<script>alert('success');</script>";
        		}
        	}
        }
    }

    private function handleContents($contents) {
    	if (!get_magic_quotes_gpc())
    		$contents = addslashes($contents);
    	return $contents;
    }
    
    private function clear_tmp_token() {
    	$sql = "truncate table tmp_token";
    	return $this->db->query($sql);
    }
    
}

?>